#include"Ticket.h"


// 全局控件
LV_FONT_DECLARE(lv_font_sunti_24);    // 导入字体
LV_FONT_DECLARE(lv_font_fan_24);
lv_obj_t *ui_dropdown_movie;
lv_obj_t *ui_label_movie_desc;
lv_obj_t *ui_btnmatrix_seats;
lv_obj_t *ui_btn_confirm;

const char *btnm_map[] = {
    "A1", "A2", "A3", "A4", "A5", "A6", "\n",  // 第一行
    "B1", "B2", "B3", "B4", "B5", "B6", "\n",  // 第二行
    "C1", "C2", "C3", "C4", "C5", "C6", "\n",  // 第三行
    "D1", "D2", "D3", "D4", "D5", "D6", ""    // 第四行（最后用空字符串 "" 结束）
};
// 电影数据
const char *movies[] = {"流浪地球", "阿凡达", "奥本海默", "蜘蛛侠", "未知"};
const char *movie_desc[] = {
    "Sci-Fi/Adventure| 120min",
    "Sci-Fi/Action | 150min",
    "Biography/History | 180min",
    "Action/Fantasy | 110min",
    "zhanwu"
};

// 场次数据
const char *sessions[] = {"10:00", "12:30", "15:00", "18:00", "20:30"};

// 座位状态（0=空闲，1=已选，2=已售）
int seat_status[4][6] = {0};

void ticket_buy()
{
    

    //2.创建容器与标签,显示标题
    lv_obj_t *panel_header = lv_obj_create(ui_Screen5);
    lv_obj_set_size(panel_header, 1024, 50);
    lv_obj_set_pos(panel_header, 0, 0);
    lv_obj_set_style_bg_color(panel_header, lv_palette_main(LV_PALETTE_GREY), 0);

    lv_obj_t *label_title = lv_label_create(panel_header);
    lv_label_set_text(label_title, "自助购票");
    lv_obj_set_style_text_font(label_title, &lv_font_fan_24, 0);
    lv_obj_center(label_title);

     // 3. 电影选择（Dropdown）
    ui_dropdown_movie = lv_dropdown_create(ui_Screen5);
    lv_obj_set_size(ui_dropdown_movie, 300, 40);
    lv_obj_set_pos(ui_dropdown_movie, 50, 70);
    lv_dropdown_set_options(ui_dropdown_movie, "流浪地球\n阿凡达\n奥本海默\n蜘蛛侠\n未知");
    lv_obj_set_style_text_font(ui_dropdown_movie, &lv_font_fan_24, 0);
    
    lv_obj_add_event_cb(ui_dropdown_movie, event_cb_movie_select, LV_EVENT_VALUE_CHANGED,NULL);

    //4. 电影描述
    ui_label_movie_desc = lv_label_create(ui_Screen5);
    lv_obj_set_pos(ui_label_movie_desc, 400, 70);
    lv_label_set_text(ui_label_movie_desc, "请选择电影");
    lv_obj_set_style_text_font(ui_label_movie_desc, &lv_font_fan_24, 0);
    
    

    // 5. 场次选择（横向按钮组）
    lv_obj_t *panel_sessions = lv_obj_create(ui_Screen5);
    lv_obj_set_size(panel_sessions, 924, 80);
    lv_obj_set_pos(panel_sessions, 50, 150);
    lv_obj_set_flex_flow(panel_sessions, LV_FLEX_FLOW_ROW);

     for (int i = 0; i < 5; i++) {
        lv_obj_t *btn = lv_btn_create(panel_sessions);
        lv_obj_set_size(btn, 120, 60);
        lv_obj_t *label = lv_label_create(btn);
        lv_label_set_text_fmt(label, "%s", sessions[i]);//调用场次数据
        lv_obj_set_style_text_font(label, &lv_font_fan_24, 0);
        
    } 

      // 4. 座位选择（Button Matrix）
    ui_btnmatrix_seats = lv_btnmatrix_create(ui_Screen5);  
    lv_obj_set_size(ui_btnmatrix_seats, 924, 250);
    lv_obj_set_pos(ui_btnmatrix_seats, 50, 250);
   

// 设置按钮地图
lv_btnmatrix_set_map(ui_btnmatrix_seats, btnm_map);
// 添加事件回调
lv_obj_add_event_cb(ui_btnmatrix_seats, event_cb_seat_select, LV_EVENT_VALUE_CHANGED,NULL);


    // 5. 底部确认按钮
    ui_btn_confirm = lv_btn_create(ui_Screen5);
    lv_obj_set_size(ui_btn_confirm, 150, 40);
    lv_obj_set_pos(ui_btn_confirm, 824, 550);
    lv_obj_set_style_bg_color(ui_btn_confirm, lv_palette_main(LV_PALETTE_ORANGE), 0);
 
    lv_obj_t *label_confirm = lv_label_create(ui_btn_confirm);
    lv_label_set_text(label_confirm, "确认购票");
    lv_obj_set_style_text_font(label_confirm, &lv_font_sunti_24, 0);
    lv_obj_center(label_confirm);
    lv_obj_add_event_cb(ui_btn_confirm, event_cb_confirm, LV_EVENT_CLICKED,NULL);

}

//电影选择
static void event_cb_movie_select(lv_event_t *e) {
    lv_obj_t *dropdown = lv_event_get_target(e);
    uint16_t selected = lv_dropdown_get_selected(dropdown);
    lv_label_set_text_fmt(ui_label_movie_desc, "%s", movie_desc[selected]);
}

// 事件回调：座位选择
static void event_cb_seat_select(lv_event_t *e) {
    lv_obj_t *btnm = lv_event_get_target(e);
    uint16_t id = lv_btnmatrix_get_selected_btn(btnm);
    if (id == LV_BTNMATRIX_BTN_NONE) return;
 
    // 计算座位行列（4行×6列）
    int row = id / 6;
    int col = id % 6;
 
    // 切换座位状态
    if (seat_status[row][col] == 0) {
        seat_status[row][col] = 1;
        lv_btnmatrix_set_btn_ctrl(btnm, id, LV_BTNMATRIX_CTRL_CHECKED);
    } else {
        seat_status[row][col] = 0;
        lv_btnmatrix_clear_btn_ctrl(btnm, id, LV_BTNMATRIX_CTRL_CHECKED);
    }
}

// 事件回调：确认购票
static void event_cb_confirm(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);
    uint16_t movie_id = lv_dropdown_get_selected(ui_dropdown_movie);
    printf("购票成功！\n");
    printf("电影: %s\n", movies[movie_id]);
    printf("座位: ");
    for (int row = 0; row < 4; row++) {
        for (int col = 0; col < 6; col++) {
            if (seat_status[row][col] == 1) {
                printf("[%c%d] ", 'A' + row, col + 1);
            }
        }
    }
    printf("\n");
}